package com.toocms.jinjuhang.ui.commodity.succour

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.data.goods.RescueDetailData
import com.toocms.jinjuhang.ui.mine.succour.ApplySuccourFgt
import com.toocms.jinjuhang.widget.view_model.checkLoginStatus
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.event.SingleLiveEvent

class SuccourDetailsModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {
    val rescueId by lazy {
        bundle?.getString(Constants.BundleKey.KEY_REFUND_ID) ?: ""
    }
    val titleSingleLiveEvent = SingleLiveEvent<String>()
    val dataObservableField = ObservableField<RescueDetailData>()
    val immediatelyDeclareClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        dataObservableField.get()?.apply {
            if (!checkLoginStatus()) return@BindingAction
            startFragment(ApplySuccourFgt::class.java, bundle)
        }

    })

    init {
        requestData(
            Urls.Goods.RESCUE_DETAIL,
            mapOf(Pair("rescue_id", rescueId)),
            resultClass = RescueDetailData::class.java,
            isShowLoading = true,
            result = {
                titleSingleLiveEvent.value = it.rescue_name
                dataObservableField.set(it)
            })
    }

}